package org.his.controller;


import java.util.Collection;
import java.util.List;

import org.his.entity.Drug;
import org.his.entity.Patient;
import org.his.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

@SessionAttributes(value = { "patient" })
@Controller
@RequestMapping("/patient")
public class PatientController {
	@Autowired
	private PatientService patientService;
	
	@RequestMapping(value = "/checkinfo", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String checkinfo(@RequestParam(value = "checkNo")String checkNo, ModelMap model) {
		// 后台信息验证
		
		Patient patientinfo = patientService.checkPatient(checkNo);
		if (patientinfo != null && patientinfo.getName() != null) {
					
				model.put("patient", patientinfo);
				// 以JSON格式向页面发送成功信息
				return "{\"success\":\"true\",\"message\":\"验证成功\"}";
			
		} else
			return "{\"success\":\"false\",\"message\":\"验证失败，请先注册\"}";
	}
	
	//增加病人
	@RequestMapping(value = "/addpatient", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addpatient(Patient addPatient) {
		// 后台信息验证
		
		int count = patientService.addPatient(addPatient);
		
		if (count == 1) {			
				// 以JSON格式向页面发送成功信息
				return "{\"success\":\"true\",\"message\":\"添加成功\"}";
			
		}else if (count == -1) {
			return "{\"success\":\"false\",\"message\":\"诊疗卡号已存在\"}";
		} else
			return "{\"success\":\"false\",\"message\":\"添加失败，请重试\"}";
	}
	//为病人信息表单提供参数
	@RequestMapping("/querybyform")
	@ResponseBody
	public Collection<Patient> querybyform(Patient qPatient){		
		List<Patient> qPatients = patientService.queryPatientByForm(qPatient);
		return qPatients;
	}
	//修改部分
		@RequestMapping(value = "/updatepatient",produces = "text/html;charset=UTF-8")
		@ResponseBody
		public String updatePatient(Patient uPatient) {
			int count = patientService.updatePatient(uPatient);
			if (count > 0) {
				return "{\"success\":\"true\",\"message\":\"病人信息修改成功\"}";
			}
			return "{\"success\":\"flase\",\"message\":\"病人信息修改失败\"}";
		}
	
}
